package cn.iocoder.yudao.module.cms.article.service;

import cn.iocoder.yudao.framework.business.category.pojo.form.CategorySaveDTO;
import cn.iocoder.yudao.framework.business.category.pojo.vo.CategoryVO;
import cn.iocoder.yudao.framework.business.category.service.CategoryBaseService;
import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission;
import cn.iocoder.yudao.module.cms.article.convert.ArticleCategoryConvert;
import cn.iocoder.yudao.module.cms.article.dal.dataobject.ArticleCategoryDO;
import cn.iocoder.yudao.module.cms.article.dal.mysql.ArticleCategoryMapper;
import cn.iocoder.yudao.module.cms.article.dal.redis.RedisKeyConstants;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Resource;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;

/**
 * 文章栏目 Service 实现类
 *
 * @author 山野羡民（1032694760@qq.com）
 */
// 注意避免与 promotion 模块的 ArticleCategoryServiceImpl 出现同名 Bean 冲突
@Service("cmsArticleCategoryServiceImpl")
@Validated
public class ArticleCategoryServiceImpl extends CategoryBaseService<ArticleCategoryDO> implements ArticleCategoryService {
    @Resource
    private ArticleCategoryMapper articleCategoryMapper;

    @CacheEvict(cacheNames = {RedisKeyConstants.ARTICLE_CATEGORY_CHILDREN_ID_LIST, RedisKeyConstants.ARTICLE_CATEGORY},
            allEntries = true) // allEntries 清空所有缓存，因为操作一个类目，涉及到多个子类目缓存
    @Override
    public Long create(CategorySaveDTO param) {
        return super.create(param);
    }

    @CacheEvict(cacheNames = {RedisKeyConstants.ARTICLE_CATEGORY_CHILDREN_ID_LIST, RedisKeyConstants.ARTICLE_CATEGORY},
            allEntries = true) // allEntries 清空所有缓存，因为操作一个类目，涉及到多个子类目缓存
    @Override
    public boolean update(CategorySaveDTO reqVO) {
        return super.update(reqVO);
    }

    @CacheEvict(cacheNames = {RedisKeyConstants.ARTICLE_CATEGORY_CHILDREN_ID_LIST, RedisKeyConstants.ARTICLE_CATEGORY},
            allEntries = true) // allEntries 清空所有缓存，因为操作一个类目，涉及到多个子类目缓存
    @Override
    public boolean delete(Long id) {
        return super.delete(id);
    }

    @Override
    @DataPermission(enable = false) // 禁用数据权限，避免建立不正确的缓存
    @Cacheable(cacheNames = RedisKeyConstants.ARTICLE_CATEGORY_CHILDREN_ID_LIST, key = "#parentId-#status")
    public Set<Long> getDescendantIdsFromCache(Long parentId, Integer status) {
        List<CategoryVO> children = getDescendantList(parentId, status);
        return convertSet(children, CategoryVO::getId);
    }

    @Override
    @Cacheable(cacheNames = RedisKeyConstants.ARTICLE_CATEGORY, key = "#id", unless = "#result == null")
    public String getCategoryNameFromCache(Long id) {
        CategoryVO category = getDetail(id);
        if (category == null) {
            return "#" + id;
        }
        return category.getName();
    }

    @Override
    public ArticleCategoryMapper getInjectedMapper() {
        return articleCategoryMapper;
    }

    @Override
    public ArticleCategoryDO convertToEntity(CategorySaveDTO param) {
        return ArticleCategoryConvert.INSTANCE.toEntity(param);
    }

    @Override
    public CategoryVO convertToViewObject(@Nonnull ArticleCategoryDO entity) {
        return ArticleCategoryConvert.INSTANCE.toViewObject(entity);
    }

}
